// pages/convert/convert.js
let App = getApp()

let test = 0

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		word: '',
		wordTypeIndex: 0,
		imgList: [[], [], [], [], []],
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(option){
		let _this = this;

		const eventChannel = _this.getOpenerEventChannel()
		
		// 发送给前面的页面
    // eventChannel.emit('Opened', {data: 'acceptDataFromOpenedPage打印的'});
  
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('Opener', function(data) {
			let word = data.word, wordTypeIndex = data.wordTypeIndex, formatIndex = data.formatIndex;
			let horizon = data.horizon, vertical = data.vertical;
			// 回调方法只能用_this
			_this.setData({word: word, wordTypeIndex: wordTypeIndex, formatIndex, horizon, vertical});
		})
		
		if (_this.data.word === '') {  // data里已经声明为空字符串了
			let word = option.word
			console.log("来自url跳转获取word", word)
			this.setData({word})
		}
		console.log("调用getWordAllImgs前展示word有没有", this.data.word)
		console.log(typeof this.data.word)
		this.getWordAllImgs()
	},

	getWordAllImgs() {  // 某个字体类型下的图片没有获取时候获取图片
		let _this = this;
		let word = _this.data.word, wordTypeIndex = _this.data.wordTypeIndex  // 不能直接用this
		console.log(word)
		App._getWithout('dic/collect/change/type', {word: word, type: wordTypeIndex}, result => {
			result = JSON.parse(result);
			console.log(result)
			
			let imgList = [];
			for (let item of result) {
				if (item['author_type'] === 0) {
					imgList.push({url: App.api_root + 'storage/dictionary/single/ancient' + item['img_url'],
				author: item['author_name']});
				} else if (item['author_type'] === 1) {
					imgList.push({url: App.api_root + 'storage/dictionary/single/modern' + item['img_url'],
				author: item['author_name']});
				} else if (item['author_type'] === 2) {
					imgList.push({url: App.api_root + 'storage/dictionary/single/user' + item['img_url'],
				author: item['author_name']});
				} else {
					imgList.push({url: App.api_root + item['img_url'], author: '云墨书法'});  // 字数据库没有图片时候的替换图片
				}
			}
			let str = 'imgList[' + wordTypeIndex + ']'
			_this.setData({[str]: imgList})
		})
	},
	
	wordTypeChange(e) {
		let _this = this;
		let wordTypeIndex = parseInt(e.currentTarget.dataset.wordTypeIndex)
		_this.setData({wordTypeIndex: wordTypeIndex}); 
		if (_this.data.imgList[wordTypeIndex].length === 0) {
			_this.getWordAllImgs()
		}
	},

	convert(e) {
		let imgUrl = e.currentTarget.dataset.url
		let author = e.currentTarget.dataset.author
		let _this = this
		wx.showModal({
			title: '换字提示',
			content: '确定换成‘' + author + '’的‘' + this.data.word + '’?',
			success (res) {
				if (res.confirm) {
					let pages = getCurrentPages();  //当前页面
					let prevPage = pages[pages.length-2];  //上一页面

					if (_this.data.formatIndex === 0) {  //集字页面是横排模式
						let str = 'matrixImg1[' + _this.data.horizon + '][' + _this.data.vertical + ']'
						prevPage.setData({[str]: {url: imgUrl, word: _this.data.word}})
					} else {
						let str = 'matrixImg2[' + _this.data.horizon + '][' + _this.data.vertical + ']'
						prevPage.setData({[str]: {url: imgUrl, word: _this.data.word}})
					}
					wx.navigateBack({//返回
						delta:1
					})		
				} else if (res.cancel) {
					console.log('用户点击取消')
				}
			}
		})
		
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})